package zhihuilan.controlle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import zhihuilan.common.Response;
import zhihuilan.config.WxMiniConfig;
import zhihuilan.entity.tools.WxSessionResult;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

//    @Resource
//    private RestTemplate restTemplate;

    @Autowired
    private WxMiniConfig wxMiniConfig;

    @GetMapping("/getOpenId")
    public Response getOpenId(@RequestParam("code") String code){
        String url = wxMiniConfig.getJscodeUrl()+"?appid="+wxMiniConfig.getAppId()+"&secret="+wxMiniConfig.getAppSecret()+"&js_code="+code+"&grant_type=authorization_code";
        RestTemplate restTemplate=new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        System.out.println("返回值为："+result);
        WxSessionResult map = JSON.parseObject(result, WxSessionResult.class);
        return Response.ok(map);
    }

    public static void main(String[] args) throws IOException {
//        JSONObject object = StringUtils.str2Json(post);
//        WxMaJscode2SessionResult sessionInfo =
//                wxMaService.getUserService().getSessionInfo(object.getString("code"));
//        QueryWrapper<MyStaff> wrapper = new QueryWrapper<>();
//        //   wrapper.eq("openid",sessionInfo.getOpenid());
//        wrapper.eq("telephone",object.getString("telephone"));
//        MyStaff staff = myStaffMapper.selectOne(wrapper);
//        if(staff==null){
//            MyStaff myStaff = new MyStaff();
//            myStaff.setOpenid(sessionInfo.getOpenid());
//            myStaff.setTelephone(object.getString("telephone"));
//            myStaff.setOpenid2(sessionInfo.getOpenid());
//            myStaff.setNickname("微信用户");
//            myStaff.setHeadimgurl("https://zhlstaticimg.oss-cn-beijing.aliyuncs.com/zhihuilan.png");
//            myStaff.setFid(0);
//            myStaffMapper.insert(myStaff);
//            myStaff = myStaffMapper.selectOne(wrapper);
//            myStaff.setGuid(Md5Utils.md5pwd(myStaff.getId()+"_"+ DateUtils.getDateTime()/1000));
//            myStaff.setNickname(myStaff.getNickname()+"_"+myStaff.getId());
//            myStaff.setUserreal(myStaff.getNickname()+"_"+myStaff.getId());
//            myStaffMapper.updateById(myStaff);
//            return myStaff;
//        }
//        return staff;
    }
}

